package com.hks360.car_treasure.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hks360.car_treasure.R;
import com.hks360.library.util.CommonUtil;


public class FourClickView extends View implements View.OnClickListener{
	private Bitmap backgroundBitmap;
	private Bitmap lowBitmap;
	private Bitmap midBitmap;
	private Bitmap highBitmap;
	private Paint paint;
	private float offx1;
	private float offx2;
	private float offx3;
	private int flag = 0;
	private float pointx;
	private int offx0;
	private OnFlagChangedListener listener;
	public void setClick(boolean click) {
		this.click = click;
	}

	private boolean click;

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public int getFlag() {
		return flag;
	}

	public FourClickView(Context context) {
		super(context);
		init();
	}

	public FourClickView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public void setOnFlagChangedListener(OnFlagChangedListener listener) {
		this.listener = listener;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
	}
	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.onoff);
		lowBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.low);
		midBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.middle);
		highBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high);
		setOnClickListener(this);
		offx0 = 50;
		offx1 = backgroundBitmap.getWidth()/4;
		offx2 = 2 * offx1;
		offx3 = backgroundBitmap.getWidth();
	}
	@Override
	protected void onDraw(Canvas canvas) {
		switch (flag) {
		case 0:
			canvas.drawBitmap(backgroundBitmap,0,0, paint);
			break;
		case 1:
			canvas.drawBitmap(lowBitmap, 0,0, paint);
			break;
		case 2:
			canvas.drawBitmap(midBitmap, 0,0, paint);
			break;
		case 3:
			canvas.drawBitmap(highBitmap, 0,0, paint);
			break;
		}
		
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			pointx = event.getX();			
			break;
		}
		return true;
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		if(click){
			if(pointx < offx0){
				flag = 0;
			}else if(pointx > offx0 && pointx < offx1 + 80){
				flag = 1;
			}else if(pointx > offx1 && pointx < offx2 + 80){
				flag = 2;
			}else if(pointx > offx2 && pointx < offx3){
				flag = 3;
			}
			listener.onFlagChanged(flag);
		}else {
			CommonUtil.showToast(getContext(), "请先启动车辆!");
		}
		invalidate();
	}

	public interface OnFlagChangedListener{
		void onFlagChanged(int flag);
	}
}
